package com.bytz.modules.cms.auditBatch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.auditBatch.model.AuditBatchModel;
import com.bytz.modules.cms.auditBatch.service.IAdminAuditBatchService;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.productLine.entity.ProductLineLevelChange;
import com.bytz.modules.cms.user.constants.AuditBatchConstants;
import com.bytz.modules.cms.user.entity.AuditBatch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "产品线等级优惠申请")
@RequestMapping("/productLineLevel/change")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.LineLevelChange.EDIT + "')")
public class LineLevelChangeController {

    @Autowired
    private IAdminAuditBatchService<ProductLineLevelChange> auditBatchLineService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<AuditBatchModel>> page(AuditBatch auditBatch,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                               HttpServletRequest req) {

        auditBatch.setType(AuditBatchConstants.Type.LINE_LEVEL);
        return Result.ok(auditBatchLineService.queryPage(
                new Page<>(pageNo, pageSize),
                QueryGenerator.initQueryWrapper(auditBatch, req.getParameterMap())
        ));
    }

    @ApiOperation(value = "查询单条", notes = "查询单条")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<AuditBatchModel<ProductLineLevelChange>> queryById(@RequestParam String id) {

        return Result.ok(auditBatchLineService.queryById(id));
    }

    @ApiOperation(value = "暂存", notes = "暂存")
    @RequestMapping(value = "/storage", method = RequestMethod.POST)
    public Result<AuditBatchModel<ProductLineLevelChange>> storage(@RequestBody AuditBatchModel<ProductLineLevelChange> model) {

        return Result.ok(auditBatchLineService.storage(model));
    }

    @ApiOperation(value = "提交", notes = "提交")
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public Result<AuditBatchModel<ProductLineLevelChange>> submit(@RequestBody AuditBatchModel<ProductLineLevelChange> model) {

        return Result.ok(auditBatchLineService.submit(model));
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(auditBatchLineService.checkAndRemoveById(id));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("产品线等级优惠申请导入模板", "Sheet1", ProductLineLevelChange.class, response);
    }

    @ApiOperation(value = "导入", notes = "导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<List<ProductLineLevelChange>> importExcel(HttpServletRequest request) {

        List<ProductLineLevelChange> models = ExcelUtil.importGroup(request, ProductLineLevelChange.class, null);
        return Result.ok(auditBatchLineService.importExcel(models));
    }
}
